package de.pharmacontrol.rfcserver.handler;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Node;

import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.server.JCoServerFunctionHandler;


public abstract class FunctionHandler implements JCoServerFunctionHandler {

	enum OrderdataMapping {
		MATNR("X01"), MAKTX("X02"), CHARG("X03"), HSDAT("X04"), VFDAT("X05"), HSDAT_PRINT("X06"), VFDAT_PRINT("X07"), 
		CHARGE_PRINT("X08"), DRUCK_INFO1("X09"), DRUCK_INFO2("X10"), DRUCK_INFO3("X11"), CIP("01C"), GS1_BATCH("10"), 
		GS1_HSDAT("11"), GS1_VFDAT("17");
	
		private String ai;
		
		private OrderdataMapping(String ai){
			this.ai = ai;
		}

		public static OrderdataMapping valueOfAi(String ai){
			for (OrderdataMapping om : values()){
				if(om.ai.equals(ai)){
					return om;
				}
			}
			return null;
		}
		
		public String getAi(){
			return this.ai;
		}
	}
	
	public final int NOT_DEFINED = 0;

	protected String getNodeValue(XPath xpath, Node node, String expr) throws XPathExpressionException{
		Object nodeObj = xpath.evaluate(expr, node, XPathConstants.NODE);
		if (nodeObj == null){
			return null;
		}
		return ((Node)nodeObj).getNodeValue();
	}
	
	protected void setReturnCode(final JCoFunction function, int ret) {
		try {
			function.getExportParameterList().setValue("E_RETURNCODE", Integer.toString(ret));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
